package com.wc.alorithm_blue_bridge._DFS.Q1216;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

/**
 * @Author congge
 * @Date 2023/10/28 20:38
 * @description 走迷宫
 * https://www.lanqiao.cn/problems/1216/learning/?page=1&first_category_id=1&sort=students_count&second_category_id=8
 */
public class Main {
    static int maxStep = 101;

    public static void main(String[] args) {
        Input sc = new Input();
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] map = new int[m + 1][n + 1];
        int[][] step = new int[m + 1][n + 1];
        for (int i = 0; i <= m; i++) {
            Arrays.fill(step[i], maxStep);
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                map[i][j] = sc.nextInt();
            }
        }
        int startI = sc.nextInt();
        int startJ = sc.nextInt();
        int endI = sc.nextInt();
        int endJ = sc.nextInt();
        if (map[startI][startJ] == 0) {
            map[startI][startJ] = 1;
        }
        Main main = new Main();
//        main.move(map, step, startI, startJ, 0, endI, endJ);
//        System.out.println(step[endI][endJ] == maxStep ? -1 : step[endI][endJ]);
        int minStep = main.move2(map, step, new boolean[m + 1][n + 1], startI, startJ, endI, endJ);
        System.out.println(minStep == maxStep ? -1 : minStep);
    }

    public void move(int[][] map, int[][] step, int i, int j, int curStep, int endI, int endJ) {
        if (isBorder(1, map.length - 1, 1, map[0].length - 1, i, j) || map[i][j] == 0) {
            return;
        }
        if (step[i][j] != maxStep && curStep >= step[i][j]) {
            return;
        }
        if (i == endI && j == endJ) {
            step[i][j] = Math.min(step[i][j], curStep);
            return;
        }
        step[i][j] = curStep;
        int up = i - 1;
        int down = i + 1;
        int left = j - 1;
        int right = j + 1;
        move(map, step, up, j, curStep + 1, endI, endJ);
        move(map, step, i, left, curStep + 1, endI, endJ);
        move(map, step, i, right, curStep + 1, endI, endJ);
        move(map, step, down, j, curStep + 1, endI, endJ);
    }

    public int move2(int[][] map, int[][] step, boolean[][] vis, int i, int j, int endI, int endJ) {
        if (isBorder(1, map.length - 1, 1, map[0].length - 1, i, j) || map[i][j] == 0) {
            return maxStep;
        }
        if (i == endI && j == endJ) {
            step[i][j] = 0;
            return 0;
        }
        if (step[i][j] != maxStep || vis[i][j]) {
            return step[i][j];
        }
        vis[i][j] = true;
        int up = i - 1;
        int down = i + 1;
        int left = j - 1;
        int right = j + 1;
        int minStep = maxStep;

        minStep = Math.min(1 + move2(map, step, vis, up, j, endI, endJ), minStep);
        minStep = Math.min(minStep, 1 + move2(map, step, vis, down, j, endI, endJ));
        minStep = Math.min(1 + move2(map, step, vis, i, left, endI, endJ), minStep);
        minStep = Math.min(1 + move2(map, step, vis, i, right, endI, endJ), minStep);
        vis[i][j] = false;
        step[i][j] = minStep;
        return step[i][j];
    }

    public boolean isBorder(int upBorder, int downBorder, int leftBorder, int rightBorder, int i, int j) {
        return i < upBorder || i > downBorder || j < leftBorder || j > rightBorder;
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
